package org.qydjk.console.controller.product;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.SingleProjectCategory;
import org.qydjk.common.pojo.product.Product;
import org.qydjk.common.pojo.product.ProductSingleProject;
import org.qydjk.common.pojo.product.ProductSingleProjectCategory;
import org.qydjk.common.util.FileUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.constant.ConsoleConstant;
import org.qydjk.console.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 产品单项管理
 * Created by Edward_yang on 2017/12/27 0027.
 */
@Controller
@RequestMapping("/productSingleProject")
public class ProductSingleProjectController extends BaseController {
    @Autowired
    private IProductService productService;
    /***
     * 跳转产品单项
     * @author Edward_yang
     * @uptime 2017-12-27
     * @return
     */
    @RequestMapping("/gotoproductSingleProject.do" )
    public String gotoproductSingleProject(){
        return "/product/productSingleProjectInfo";
    }

    /***
     * 产品列表查询
     * @author Edward_yang
     * @uptime 2017-12-26
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/productList.do")
    @ResponseBody
    public Object productList(Integer page,Integer rows,String keyWord){
        try {
            return productService.findProductAllList(page,rows,keyWord);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取单项库分类列表
     * @author Edward_yang
     * @uptime 2017-12-20
     */
    @RequestMapping("/singleProjectCategoryList.do")
    @ResponseBody
    public Object singleProjectCategoryList( Integer page,Integer rows,Integer productId,String keyWord) {
        List<Map<String,Object>>   singleProjectCategoryList;//全部分类
        List<Map<String,Object>>   productSingleProjectCategoryList;//产品已配单项分类
        try {
            String sql = "SELECT  qspc.id as id , qspc.name as name ,qspc.ord as ord ,qspc.status as status FROM qy_single_project_category qspc WHERE qspc.status=1";
            String sql1="SELECT qpspc.single_project_category_id as  categoryId  FROM qy_product_single_project_category qpspc WHERE  product_id="+productId;
            productSingleProjectCategoryList=  baseService.findMapBySQL(sql1);
            String countSql = "select count(qspc.id) from qy_single_project_category qspc";
            if(!StringUtil.isEmptyOrNull(keyWord)){
                sql+="  and qspc.name like '%"+keyWord+"%' ";
                countSql+=" and qspc.name like '%"+keyWord+"%'";
            }
            sql = sql + " order by ord desc";
            singleProjectCategoryList = baseService.findMapBySQL(sql,new HibernatePage(page, rows));
          for(Map map:singleProjectCategoryList){
               if (productSingleProjectCategoryList!=null&& productSingleProjectCategoryList.size()!=0){
                   outer: for(Map map1: productSingleProjectCategoryList){
                        if(map.get("id").toString().equals(map1.get("categoryId").toString())){
                            map.put("statusType",1);//已配置
                            map.put("type",1);//已配置
                            map.put("productId",productId);
                           break outer;
                        }else{
                            map.put("statusType",0);//未配置
                            map.put("type",0);//未配置
                            map.put("productId",productId);
                        }
                    }
                }else{
                    map.put("statusType",0);//未配置
                    map.put("type",0);//未配置
                    map.put("productId",productId);
                }
            }
            System.out.println(singleProjectCategoryList);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total, singleProjectCategoryList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /****
     * 点击配置单项分类(添加)
     * @author Edward_yang
     * @uptime 2017-12-28
     */
    @RequestMapping("/checkConfiguration.do")
    @ResponseBody
    public Object checkConfiguration(Integer productId,Integer singleProjectCategoryId){
        ProductSingleProjectCategory productSingleProjectCategory=new ProductSingleProjectCategory();
        productSingleProjectCategory.setProductId(productId);
        productSingleProjectCategory.setSingleProjectCategoryId(singleProjectCategoryId);
        productSingleProjectCategory.setCreateTime(new Date());
        productSingleProjectCategory.setUpdateTime(new Date());
        try {
            baseService.save(productSingleProjectCategory);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /***
     * 取消配置分类(删除)
     * @author Edward_yang
     * @uptime 2017-12-28
     * @return
     */
    @RequestMapping("/deletecheckConfiguration.do")
    @ResponseBody
    public Object deletecheckConfiguration(Integer productId,Integer singleProjectCategoryId){
        try {
            List<ProductSingleProject>  ProductSingleProjectList=baseService.findBeanBySQL("SELECT *FROM qy_product_single_project  WHERE product_id="+productId+" and single_project_category_id="+singleProjectCategoryId,ProductSingleProject.class);
            if(ProductSingleProjectList.size()>0){
                return renderError("请取消分类下的单项的配置");
            }
            baseService.excuteSQL("DELETE FROM qy_product_single_project_category WHERE product_id="+productId+" and single_project_category_id="+singleProjectCategoryId);//删除那一条数据
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }


    /**
     * 获取产品单项列表
     * @author Edward_yang
     * @uptime 2017-12-20
     */
    @RequestMapping("/productSingleProjectList.do")
    @ResponseBody
    public Object productSingleProjectList( Integer page,Integer rows,Integer productId,Integer categoryId,String keyWord) {
        List<Map<String,Object>>   singleProjectList;//全部单项
        List<Map<String,Object>>   productSingleProjectList;//产品已配单项分类
        try {
            String sql = "SELECT  qsp.id as id , qsp.single_project_category_id as singleProjectCategoryId ,qsp.name as name,qsp.price as price,qsp.des as des,qsp.significance as significance,qsp.ord as ord ,qsp.status as status FROM qy_single_project qsp  WHERE   qsp.single_project_category_id="+categoryId;
            String sql1="SELECT qpsp.single_project_id as  singleProjectId  FROM qy_product_single_project qpsp WHERE product_id="+productId;
            productSingleProjectList= baseService.findMapBySQL(sql1);
            String countSql = "select count(id) from qy_single_project_category";

            if(!StringUtil.isEmptyOrNull(keyWord)){
                sql+="  and qsp.name like '%"+keyWord+"%' ";
                countSql+=" and qsp.name like '%"+keyWord+"%'";
            }
            sql = sql + " order by ord desc";
            singleProjectList = baseService.findMapBySQL(sql, new HibernatePage(page, rows));
         for(Map map:singleProjectList){
                if (productSingleProjectList!=null&& productSingleProjectList.size()!=0){
                    outer: for(Map map1: productSingleProjectList){
                        if(map.get("id").toString().equals(map1.get("singleProjectId").toString())){
                            map.put("statusType",1);//已配置
                            map.put("type",1);//已配置
                            map.put("productId",productId);
                            break outer;
                        }else{
                            map.put("statusType",0);//未配置
                            map.put("type",0);//未配置
                            map.put("productId",productId);
                        }
                    }
                }else{
                    map.put("statusType",0);//未配置
                    map.put("type",0);//未配置
                    map.put("productId",productId);
                }
            }
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total, singleProjectList);
        } catch (Exception e) {
            e.printStackTrace();
        }
     return null;
    }

    /****
     * 点击配置单项(添加)
     * @author Edward_yang
     * @uptime 2017-12-28
     * @return
     */
    @RequestMapping("/checkSingleProject.do")
    @ResponseBody
    public Object checkSingleProject(Integer productId,Integer singleProjectCategoryId,  Integer singleProjectId){
        try {
            String sql="SELECT qpspc.single_project_category_id as  categoryId  FROM qy_product_single_project_category qpspc WHERE  product_id="+productId+" and single_project_category_id="+singleProjectCategoryId;
            List<Map<String,Object>>   productSingleProjectCategoryList;//产品已配单项分类
            productSingleProjectCategoryList=baseService.findMapBySQL(sql);
            if(productSingleProjectCategoryList.size()<=0){
                return renderError("请配置该单项的分类");
            }
            ProductSingleProject productSingleProject=new   ProductSingleProject();
            productSingleProject.setProductId(productId);
            productSingleProject.setSingleProjectId(singleProjectId);
            productSingleProject.setSingleProjectCategoryId(singleProjectCategoryId);
            productSingleProject.setCreateTime(new Date());
            productSingleProject.setUpdateTime(new Date());
            baseService.save(productSingleProject);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /***
     *取消配置单项(删除)
     * @author Edward_yang
     * @uptime 2017-12-28
     * @return
     */
    @RequestMapping("/deleteCheckSingleProject.do")
    @ResponseBody
    public Object deleteCheckSingleProject(Integer productId,Integer singleProjectId){
        try {
            baseService.excuteSQL("DELETE FROM qy_product_single_project WHERE product_id="+productId+" and single_project_id="+singleProjectId);//删除那一条数据
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError(e.getMessage());
        }
    }

    /****
     * 生成单项组合html 返回页面显示
     * @author Edward_yang
     * @uptime 2017-12-29
     * @return
     */
    @RequestMapping("/createSingleProject.do")
    @ResponseBody
    public  Object  createSingleProject(Integer productId){
        try {
        //查出产品名称
        Product product = baseService.get(Product.class, productId);
            System.out.println(product.getName());
            StringBuffer productDetail = new StringBuffer("");
        //配置单项分类数据
         List<Map<String,Object>> groupSingleProjectCategoryList;
            String sql = "   SELECT  qspc.id as id , qspc.name as name ,qspc.ord as ord  " +
                          "   FROM qy_single_project_category qspc " +
                          "   WHERE " +
                          "   qspc.status=1  " +
                          "   AND qspc.id in (SELECT qpspc.single_project_category_id as  categoryId  FROM qy_product_single_project_category qpspc WHERE  product_id="+productId+") " +
                          "   order by ord desc ";
            groupSingleProjectCategoryList= baseService.findMapBySQL(sql);
            System.out.println(groupSingleProjectCategoryList);

            //one for 循环开始
            for(Map map:groupSingleProjectCategoryList){
                System.out.println("分类ID:"+map.get("id"));

                //配置单项数据
                List<Map<String,Object>> groupSingleProjectList;
                String sql2="  SELECT  qsp.id AS id,qsp.single_project_category_id AS singleProjectCategoryId, qsp. name AS name,qsp.des AS des,qsp.significance AS significance,qsp.ord AS ord " +
                        "  FROM qy_single_project qsp" +
                        "  WHERE" +
                        "  qsp.status = 1 " +
                        "  AND  qsp.id IN (SELECT qpsp.single_project_id AS singleProjectId FROM qy_product_single_project qpsp WHERE qpsp.product_id = "+productId+") " +
                        "  AND qsp.single_project_category_id = "+map.get("id").toString()+
                        "  order by ord desc";
                groupSingleProjectList= baseService.findMapBySQL(sql2);
                System.out.println(groupSingleProjectList);
                //导入分类
                productDetail.append(" <tr>\n" +
                        "            <td colspan=\"3\" style=\"line-height: 22px;\n" +
                        "\t\t\t    font-size: 12px;\n" +
                        "\t\t\t    color: #666;\n" +
                        "\t\t\t    border-bottom: #ddd 1px solid;\n" +
                        "\t\t\t    border-right: #dfdfdf 1px solid;\n" +
                        "\t\t\t    text-align: left;\n" +
                        "\t\t\t    padding-left: 15px;\n" +
                        "\t\t\t    padding-top: 13px;\n" +
                        "\t\t\t    padding-bottom: 11px;\n" +
                        "\t\t\t    position: relative;text-align: left; padding-left: 15px; font-size: 16px; color: #019eeb; background-color: #f3f3f3;\">"+map.get("name").toString()+"("+groupSingleProjectList.size()+"项)</td>\n" +
                        "        </tr>");
                  //two for 循环开始
                for(Map map1:groupSingleProjectList){
                    productDetail.append(" <tr>\n" +
                            "            <td class=\"td_title\" style=\"line-height: 22px;\n" +
                            "\t\t\t    font-size: 12px;\n" +
                            "\t\t\t    color: #666;\n" +
                            "\t\t\t    border-bottom: #ddd 1px solid;\n" +
                            "\t\t\t    border-right: #dfdfdf 1px solid;\n" +
                            "\t\t\t    text-align: left;\n" +
                            "\t\t\t    padding-left: 15px;\n" +
                            "\t\t\t    padding-top: 13px;\n" +
                            "\t\t\t    padding-bottom: 11px;\n" +
                            "\t\t\t    position: relative;width: 110px\" rowspan=\"1\">"+map1.get("name").toString()+"</td>\n" +
                            "            <td class=\"\" style=\"line-height: 22px;\n" +
                            "\t\t\t    font-size: 12px;\n" +
                            "\t\t\t    color: #666;\n" +
                            "\t\t\t    border-bottom: #ddd 1px solid;\n" +
                            "\t\t\t    border-right: #dfdfdf 1px solid;\n" +
                            "\t\t\t    text-align: left;\n" +
                            "\t\t\t    padding-left: 15px;\n" +
                            "\t\t\t    padding-top: 13px;\n" +
                            "\t\t\t    padding-bottom: 11px;\n" +
                            "\t\t\t    position: relative;line-height: 22px;\n" +
                            "\t\t\t    font-size: 12px;\n" +
                            "\t\t\t    color: #666;\n" +
                            "\t\t\t    border-bottom: #ddd 1px solid;\n" +
                            "\t\t\t    border-right: #dfdfdf 1px solid;\n" +
                            "\t\t\t    text-align: left;\n" +
                            "\t\t\t    padding-left: 15px;\n" +
                            "\t\t\t    padding-top: 13px;\n" +
                            "\t\t\t    padding-bottom: 11px;\n" +
                            "\t\t\t    position: relative;width: 137px;\">"+map1.get("des").toString()+"</td>\n" +
                            "            <td class=\"\" style=\"line-height: 22px;\n" +
                            "\t\t\t    font-size: 12px;\n" +
                            "\t\t\t    color: #666;\n" +
                            "\t\t\t    border-bottom: #ddd 1px solid;\n" +
                            "\t\t\t    border-right: #dfdfdf 1px solid;\n" +
                            "\t\t\t    text-align: left;\n" +
                            "\t\t\t    padding-left: 15px;\n" +
                            "\t\t\t    padding-top: 13px;\n" +
                            "\t\t\t    padding-bottom: 11px;\n" +
                            "\t\t\t    position: relative;line-height: 22px;\n" +
                            "\t\t\t    font-size: 12px;\n" +
                            "\t\t\t    color: #666;\n" +
                            "\t\t\t    border-bottom: #ddd 1px solid;\n" +
                            "\t\t\t    border-right: #dfdfdf 1px solid;\n" +
                            "\t\t\t    text-align: left;\n" +
                            "\t\t\t    padding-left: 15px;\n" +
                            "\t\t\t    padding-top: 13px;\n" +
                            "\t\t\t    padding-bottom: 11px;\n" +
                            "\t\t\t    position: relative;width: 137px;\">"+map1.get("significance").toString()+"</td>\n" +
                            "        </tr>");
                }
            }
            String html = FileUtil.readFromSysPath(ConsoleConstant.SINGLE_PROJECT_PRODUCT_DETAIL_TEMPLATE_PATH);

            html = html.replace(ConsoleConstant.SINGLE_PROJECT_PRODUCT_DETAIL_TEMPLATE_CONTENT_NAME,product.getName()).replace(ConsoleConstant.SINGLE_PROJECT_PRODUCT_DETAIL_TEMPLATE_CONTENT_DETAIL,productDetail);
            System.out.println(html);
            return renderSuccess(html);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /***
     * 保存生成单项
     * @author Edward_yang
     * @uptime 2018-01-02
     * @return
     */
    @RequestMapping("saveSettingsSingleProject.do")
    @ResponseBody
    public  Object  saveSettingsSingleProject(Integer productId ,String detail){
        try {
            Product product = baseService.get(Product.class, productId);
            product.setDetail(detail);
            baseService.update(product);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
        return renderSuccess();
    }
}
